<template>
  <div>
    <el-card>
      <el-container>
        <el-header>
          <el-row>
            <div style="margin-bottom: 15px">
              <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/admin' }">
                  首页
                </el-breadcrumb-item>
                <el-breadcrumb-item>
                  <a href="/">注册申请审核</a>
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </el-row>
          <el-row :gutter="30">
            <el-col :span="12" style="text-align: left">
              <div>
                <el-select v-model="queryType" @change="queryList" popper-class="modifySelect">
                  <el-option label="药品生产商" value="1"></el-option>
                  <el-option label="药品经销商" value="2"></el-option>
                  <!-- <el-option label="药品消费者" value="3"></el-option> -->
                </el-select>
                <el-button
                  @click="queryList"
                  style="background-color: #409eff; color: #eeeeee"
                >
                  查询
                </el-button>
              </div>
            </el-col>
          </el-row>
        </el-header>
        <el-main>
          <el-table
            v-if="queryType == 1 || queryType == 2"
            :data="userList"
            border
            style="width: 100%"
          >
            <el-table-column
              fixed
              prop="CompanyName"
              label="企业/商家名称"
              width="150"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="LegalPerson" label="法人姓名" align="center">
            </el-table-column>
            <el-table-column
              prop="License"
              label="经营许可证编号"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="GMPID"
              label="药品GMP / GSP证书编号"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="Phone" label="手机号码" align="center">
            </el-table-column>
            <el-table-column prop="CreatedAt" label="申请时间" align="center">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  @click="accept(scope.row.ID)"
                  type="text"
                  size="small"
                >
                  通过
                </el-button>
                <el-button
                  @click="reject(scope.row.ID)"
                  type="text"
                  size="small"
                >
                  不通过
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-footer style="text-align: center; margin-top: 20px">
            <el-pagination
              style="color: cadetblue"
              background
              layout="prev, pager, next"
              :total="total"
            >
            </el-pagination>
          </el-footer> -->

          <!-- 消费者 -->
          <!-- <el-table
            v-show="queryType == 3"
            :data="userList"
            stripe=""
            border
            style="width: 100%"
          >
            <el-table-column
              fixed
              prop="Name"
              label="用户名"
              width="150"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="IdNumber" label="身份证号码" align="center">
            </el-table-column>
            <el-table-column prop="Phone" label="手机号码" align="center">
            </el-table-column>
            <el-table-column prop="CreatedAt" label="申请时间" align="center">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center">
              <template slot-scope="scope">
                <div>
                  <el-button
                    @click="accept(scope.row.ID)"
                    type="text"
                    size="small"
                  >
                    通过
                  </el-button>
                  <el-button
                    @click="reject(scope.row.ID)"
                    type="text"
                    size="small"
                  >
                    不通过
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table> -->
          
          <!-- <el-footer style="text-align: center; margin-top: 20px">
            <el-pagination
              style="color: cadetblue"
              background
              layout="prev, pager, next"
              :total="total"
            >
            </el-pagination>
          </el-footer> -->
        </el-main>
      </el-container>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://localhost:7500";
export default {
  name: "Admin",
  data() {
    return {
      queryType: "1",
      userList: []
    };
  },
  created() {
    this.queryList();
  },
  methods: {
    queryList() {
      if (this.queryType == 1) {
        axios({
          url: "/api/v2/pass/manufacturer",
          method: "get",
          headers: {
            token: JSON.parse(window.sessionStorage.getItem("adminInfo")).token
          }
        })
          .then((res) => {
            console.log(res);
            if (res.data.code !== 0) throw new Error("获取信息失败!");
            this.userList = res.data.data;
          })
          .catch(() => {
            return this.$message.error("获取药品生产商列表失败!");
          });
      } else if (this.queryType == 2) {
        axios({
          url: "/api/v2/pass/distributor",
          method: "get",
          headers: {
            token: JSON.parse(window.sessionStorage.getItem("adminInfo")).token
          }
        })
          .then((res) => {
            console.log(res);
            if (res.data.code !== 0) throw new Error("获取信息失败!");
            this.userList = res.data.data;
          })
          .catch(() => {
            return this.$message.error("获取药品生产商列表失败!");
          });
      } else if (this.queryType == 3) {
        axios({
          url: "/api/v2/pass/consumer",
          method: "get",
          headers: {
            token: JSON.parse(window.sessionStorage.getItem("adminInfo")).token
          }
        })
          .then((res) => {
            console.log(res);
            if (res.data.code !== 0) throw new Error("获取信息失败!");
            this.userList = res.data.data;
          })
          .catch(() => {
            return this.$message.error("获取药品生产商列表失败!");
          });
      }
    },
    async accept(id) {
      if (this.queryType == 1) {
        const { data: res } = await axios.put(
          "api/v2/pass/manufacturer",
          {
            wallet: "",
            id: id,
            state: 1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      } else if (this.queryType == 2) {
        const { data: res } = await axios.put(
          "api/v2/pass/distributor",
          {
            wallet: "",
            id: id,
            state: 1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      } else if (this.queryType == 3) {
        const { data: res } = await axios.put(
          "api/v2/pass/consumer",
          {
            wallet: "",
            id: id,
            state: 1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      }
    },
    async reject(id) {
      if (this.queryType == 1) {
        const { data: res } = await axios.put(
          "api/v2/pass/manufacturer",
          {
            id: id,
            state: -1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      } else if (this.queryType == 2) {
        const { data: res } = await axios.put(
          "api/v2/pass/distributor",
          {
            wallet: "",
            id: id,
            state: -1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      } else if (this.queryType == 3) {
        const { data: res } = await axios.put(
          "api/v2/pass/consumer",
          {
            wallet: "",
            id: id,
            state: -1
          },
          {
            headers: {
              token: JSON.parse(window.sessionStorage.getItem("adminInfo"))
                .token
            }
          }
        );
        console.log(res);
        if (res.code !== 0) return this.$message.error("操作失败!");
        this.$message.success("操作成功!");
        this.queryList();
      }
    }
  }
};
</script>

<style lang="less" scope>
.modifySelect{
  height: 90px;
}
.el-select-dropdown__item{
  height:45px;
}
</style>
